package com.googlelogin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;

import com.propertypoint.presentation.model.Agent;
import com.propertypoint.presentation.model.BaseUser;
import com.propertypoint.presentation.model.GoogleUser;
import com.propertypoint.presentation.model.KeyValue;
import com.propertypoint.service.business.AuditServiceImpl;
import com.propertypoint.service.business.UserLoginServiceImpl;
import com.propertypoint.service.general.AuditService;
import com.propertypoint.service.general.UserLoginService;
import com.propertypoint.util.PropertyPointConstants;


/**
 * Servlet implementation class GoogleCallbackServlet
 */
@WebServlet("/GoogleCallbackServlet")
public class GoogleCallbackServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserLoginService userLoginService = new UserLoginServiceImpl();   
	private AuditService auditService=new AuditServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GoogleCallbackServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String code = request.getParameter("code");
		String state = request.getParameter("state");
		if (null != code) {
			GoogleAuthHelper helper = new GoogleAuthHelper();
		    helper.setUp();
		    GoogleAuthHelper.exchangeCode(code);
			//helper.createEvent(code);
			GoogleUser googleUser = helper.getUserInfoJson();
			 
			try {
				Agent user = (Agent) userLoginService.getUser(googleUser
						.getEmail());
				if(user!=null){
				request.getSession().setAttribute("googleUser", googleUser);
				request.getSession()
						.setAttribute("google_loging_success", true);
				request.getSession().setAttribute("googleCode", code);
				request.getSession().setAttribute("login_status", "true");
				request.getSession().setAttribute("user",user);
				List<KeyValue> pieDataList =auditService.getSearchHitsHDBLocation();
				request.getSession().setAttribute("pieDataListHDBHits", pieDataList);
				response.sendRedirect(request.getContextPath() + "/home.jsp");
				}
				else {
					request.getSession().setAttribute("login_status", "false");
					request.setAttribute("login_error", "User has not registered with Property Point!");
					RequestDispatcher rd = getServletContext().getRequestDispatcher(
							PropertyPointConstants.HOME_PAGE);
					rd.forward(request, response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println("jsonUser : " + googleUser);
		} else {

			response.sendRedirect(request.getContextPath() + "/error.html");
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	


}
